package com.moxiao.yfjh.module.member.dal.mysql.point;

import com.moxiao.yfjh.framework.common.pojo.PageParam;
import com.moxiao.yfjh.framework.common.pojo.PageResult;
import com.moxiao.yfjh.framework.mybatis.core.mapper.BaseMapperX;
import com.moxiao.yfjh.framework.mybatis.core.query.LambdaQueryWrapperX;
import com.moxiao.yfjh.module.member.controller.admin.point.vo.recrod.MemberPointRecordPageReqVO;
import com.moxiao.yfjh.module.member.dal.dataobject.point.MemberPointRecordDO;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;

import java.util.List;
import java.util.Set;

/**
 * 用户积分记录 Mapper
 *
 * @author QingX
 */
@Mapper
public interface MemberPointRecordMapper extends BaseMapperX<MemberPointRecordDO> {

    default PageResult<MemberPointRecordDO> selectPage(MemberPointRecordPageReqVO reqVO, Set<Long> userIds) {
        return selectPage(reqVO, new LambdaQueryWrapperX<MemberPointRecordDO>()
                .inIfPresent(MemberPointRecordDO::getUserId, userIds)
                .eqIfPresent(MemberPointRecordDO::getUserId, reqVO.getUserId())
                .eqIfPresent(MemberPointRecordDO::getBizType, reqVO.getBizType())
                .likeIfPresent(MemberPointRecordDO::getTitle, reqVO.getTitle())
                .orderByDesc(MemberPointRecordDO::getId));
    }

    default PageResult<MemberPointRecordDO> selectPage(Long userId, PageParam pageVO) {
        return selectPage(pageVO, new LambdaQueryWrapperX<MemberPointRecordDO>()
                .eq(MemberPointRecordDO::getUserId, userId)
                .orderByDesc(MemberPointRecordDO::getId));
    }

    /**
     * 查询用户积分
     * @param userId
     * @return
     */
    @Select("SELECT sum(point) FROM member_point_record WHERE user_id = #{userId}")
    Long selectUserPoint(@Param("userId") Long userId);

    /**
     * 查询小于0的数据
     * @param userId
     * @return
     */
    default List<MemberPointRecordDO> selectLeZero(Long userId){
        return selectList(new LambdaQueryWrapperX<MemberPointRecordDO>()
                .lt(MemberPointRecordDO::getPoint, 0)
                .eq(MemberPointRecordDO::getUserId, userId));
    }


}
